import { get, post, put } from '@/utils/request';
import type { User, LoginResponse } from '@/types';

/**
 * 登录
 */
export const login = (email: string, password: string, tenantId?: string) => {
  return post<LoginResponse>('/auth/login', { email, password, tenantId });
};

/**
 * 登出
 */
export const logout = () => {
  return post('/auth/logout');
};

/**
 * 获取当前用户信息
 */
export const getCurrentUser = () => {
  return get<User>('/auth/me');
};

/**
 * 获取当前用户权限
 */
export const getCurrentUserPermissions = () => {
  return get<{ permissions: string[]; isSuperAdmin: boolean }>('/auth/permissions');
};

/**
 * 修改密码
 */
export const changePassword = (oldPassword: string, newPassword: string) => {
  return post('/auth/change-password', { oldPassword, newPassword });
};

/**
 * 获取可用租户列表（公开接口）
 */
export const getAvailableTenants = () => {
  return get<Array<{ id: string; name: string; subdomain: string }>>('/auth/tenants');
};

/**
 * 更新当前用户信息
 */
export const updateCurrentUser = (data: {
  firstName?: string;
  lastName?: string;
  avatar?: string;
}) => {
  return put('/auth/me', data);
};